package cc.lj.internal.coupons.interfaces.dto.convert;

import cc.lj.internal.coupons.domain.martcap.model.vo.CapBatchDO;
import cc.lj.internal.coupons.interfaces.dto.CapBatchDTO;
import cc.lj.internal.coupons.interfaces.dto.command.InsertCapBatchCommand;
import cc.lj.internal.coupons.interfaces.dto.command.UpdateCapBatchCommand;
import cc.lj.internal.coupons.interfaces.dto.query.CapBatchQuery;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;

import java.math.BigDecimal;

/**
 * 封顶额好批次表DO与DTO转换
 * @author liangxifeng
 * @date 2021-12-13
 */
@Mapper
public interface CapBatchDTOConvertDO {
    CapBatchDTOConvertDO INSTANCE = Mappers.getMapper(CapBatchDTOConvertDO.class);

    /**
     * DTO转DO时，卖场封顶额占比，商户和商城分摊比需要格式化为：/100
     */
    @Mapping(source = "merchantShare",target = "merchantShare",qualifiedByName = "merchantShareDivide")
    @Mapping(source = "martShare",target = "martShare",qualifiedByName = "martShareDivide")
    @Mapping(source = "martCapRatio",target = "martCapRatio",qualifiedByName = "martShareDivide")
    CapBatchDO convert(InsertCapBatchCommand cmd);

    /**
     * DTO转DO时，卖场封顶额占比，商户和商城分摊比需要格式化为：/100
     */
    @Mapping(source = "merchantShare",target = "merchantShare",qualifiedByName = "merchantShareDivide")
    @Mapping(source = "martShare",target = "martShare",qualifiedByName = "martShareDivide")
    @Mapping(source = "martCapRatio",target = "martCapRatio",qualifiedByName = "martCapRatioDivide")
    @Mapping(target = "batchId",ignore = true)
    //dto to do
    CapBatchDO convert(UpdateCapBatchCommand cmd);
    //dto to do
    CapBatchDO convert(CapBatchQuery cmd);

    /**
     * DO转DTO时，卖场封顶额占比，中的商户和商城分摊比需要格式化为：*100
     * @param capBatchDO
     * @return
     */
    @Mapping(source = "merchantShare",target = "merchantShare",qualifiedByName = "merchantShareMutiply")
    @Mapping(source = "martShare",target = "martShare",qualifiedByName = "martShareMutiply")
    @Mapping(source = "martCapRatio",target = "martCapRatio",qualifiedByName = "martCapRatioMutiply")
    //do to dto
    CapBatchDTO convert(CapBatchDO capBatchDO);

    /**
     * 商户分摊比例 *100
     * @param merchantShare 商户分摊比
     * @return 商户分摊比*100
     */
    @Named("merchantShareMutiply")
    default BigDecimal merchantShareMutiply(BigDecimal merchantShare) {
        if(merchantShare != null) {
            return merchantShare.multiply(new BigDecimal(100));
        } else {
            return null;
        }
    }
    /**
     * 最高占卖场封顶额比例 *100
     * @param martCapRatio 最高占卖场封顶额比例
     * @return 最高占卖场封顶额比例*100
     */
    @Named("martCapRatioMutiply")
    default BigDecimal martCapRatioMutiply(BigDecimal martCapRatio) {
        if(martCapRatio != null) {
            return martCapRatio.multiply(new BigDecimal(100));
        } else {
            return null;
        }
    }
    /**
     * 卖场分摊比例 *100
     * @param martShare
     * @return
     */
    @Named("martShareMutiply")
    default BigDecimal martShareMutiply(BigDecimal martShare) {
        if(martShare != null) {
            return martShare.multiply(new BigDecimal(100));
        } else {
            return null;
        }
    }
    /**
     * 商户分摊比例 /100
     * @param merchantShare
     * @return
     */
    @Named("merchantShareDivide")
    default BigDecimal merchantShareDivide(BigDecimal merchantShare) {
        if(merchantShare != null) {
            return merchantShare.divide(new BigDecimal(100));
        } else {
            return null;
        }
    }
    /**
     * 卖场分摊比例 /100
     * @param martShare
     * @return
     */
    @Named("martShareDivide")
    default BigDecimal martShareDivide(BigDecimal martShare) {
        if(martShare != null) {
            return martShare.divide(new BigDecimal(100));
        } else {
            return null;
        }
    }
    /**
     * 占卖场最高封顶额比例 /100
     * @param martCapRatio 占卖场最高封顶额比例
     * @return 占卖场最高封顶额比例 /100
     */
    @Named("martCapRatioDivide")
    default BigDecimal martCapRatioDivide(BigDecimal martCapRatio) {
        if(martCapRatio != null) {
            return martCapRatio.divide(new BigDecimal(100));
        } else {
            return null;
        }
    }
}
